const { default: axios } = require("axios");

function lookupIpGetter() {
  return axios.get('https://ipv4.lookup.test-ipv6.com/ip/').then(res => {
    if(res.data) {
      return res.data.ip;
    }
  })
}

function ipApiGetter() {
  return axios.get('http://ip-api.com/json/?fields=query').then(res=>{
    if(res.data) {
      return res.data.query;
    }
  })
}

function taobaoIpGetter() {

}


function $3322IpGetter() {
  return axios.get('http://ip.3322.net/').then(res=>{
    return res.data;
})
}

const ipGetterList =[lookupIpGetter,ipApiGetter,$3322IpGetter];

function ipGetter() {
  let ipList =new Array(4);
  ipGetterList.map((it,index)=>{
    it().then(ip=>{
      ipList[index] = ip;
    }).catch(err=>{
      console.log(err);
    })
  })
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const hasFetchedIpList = ipList.filter(it=>typeof(it) === 'string')
      if(hasFetchedIpList.length>0) {
        resolve(hasFetchedIpList[0]);
      } else {
        console.log('ip获取失败');
        reject('获取ip失败')
      }
    }, 5000);
  });
}
ipGetter()
module.exports = ipGetter
